<template>
  <div>
    <h2>欢迎访问蜗牛养老后台管理系统</h2>
    <el-card>
      <!--搜索框11-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入长者姓名" clearable v-model="name" @clear="clearSearch">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openAddChange">更换护理等级</el-button>
        </el-col>
      </el-row>
      <!--添加外出申请-->
      <el-dialog title="创建更护记录" :visible.sync="addChangeFormVisible" @close="closeAddChangeForm"
                 :close-on-click-modal="false">
        <el-dialog
            width="75%"
            title="选择长者信息"
            :visible.sync="innerVisible"
            append-to-body>
          <el-input placeholder="请输入长者姓名" clearable v-model="olderName" style="width: 450px"
                    @clear="searchOlder">
            <el-button slot="append" icon="el-icon-search" @click="searchOlder">搜索长者</el-button>
          </el-input>
          <el-pagination
              @size-change="olderSizeChange"
              @current-change="olderCurrentChange"
              :current-page="olderPageNum"
              :page-sizes="[3, 6 , 12, 24]"
              :page-size="olderPageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="olderTotal"
          ></el-pagination>
          <template>
            <el-table
                :data="olderList"
                style="width: 100%"
                :row-class-name="tableRowClassName"
            >
              <el-table-column
                  label="编号"
                  type="index"
              >
              </el-table-column>
              <el-table-column
                  prop="name"
                  label="长者姓名"
              >
              </el-table-column>
              <el-table-column
                  prop="sex"
                  label="性别"
              >
              </el-table-column>
              <el-table-column
                  prop="bed.bedname"
                  label="床位号"
              >
              </el-table-column>
              <el-table-column
                  prop="idcard"
                  label="长者身份证号"
              >
              </el-table-column>
              <el-table-column
                  prop="checkdate"
                  label="入住日期"
              >
              </el-table-column>
              <el-table-column
                  label="在院状态"
              >
                <template slot-scope="scope">
                  <span style="color: red" v-if="scope.row.status == 0">退房</span>
                  <span style="color: green" v-if="scope.row.status == 1">入住</span>
                  <span style="color: red" v-if="scope.row.status == 2">待退房</span>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      v-if="scope.row.status != 0"
                      @click="selectOlderAdd(scope.row.id,scope.row.name)">选择
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-dialog>
        <el-form ref="addChangeForm" :rules="addChangeFormRules" :model="addChangeForm" label-width="150px" size="mini">
          <el-form-item label="长者:" prop="name" @click="innerVisible = true">
            <el-button type="primary" v-model="addChangeForm.olderId" @click="innerVisible = true">
              {{ addChangeForm.name }}
            </el-button>
          </el-form-item>
          <el-form-item label="现护理等级:" prop="oldercareId">
            <span>{{ addChangeForm.olderCareName }}</span>
          </el-form-item>
          <el-form-item label="新护理等级:" prop="newcareId">
            <el-select v-model="addChangeForm.newcareId" placeholder="请选择合同类型">
              <el-option v-for="c in CareList" :label="c.level" :value="c.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="更换日期:" prop="changedatetime">
            <el-date-picker
                v-model="addChangeForm.changedatetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :editable="false"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注:" prop="changeremark">
            <el-input v-model="addChangeForm.changeremark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddChangeForm">取 消</el-button>
          <el-button type="primary" @click="addChangeFormOK">确 定</el-button>
        </div>
      </el-dialog>
      <!--主体数据-->

      <el-table
          :data="ChangeCareList"
          style="width: 100%"
          :row-class-name="tableRowClassName"

      >
        <el-table-column
            label="编号"
            type="index"
            align="center">
        </el-table-column>
        <el-table-column
            prop="older.name"
            align="center"
            label="长者姓名">
        </el-table-column>
        <el-table-column
            prop="older.idcard"
            align="center"
            label="长者身份证号">
        </el-table-column>
        <el-table-column
            prop="oldercareId"
            align="center"
            label="原护理级别">
          <template slot-scope="scope">
            <span style="color: #2d3a4b" v-for="c in CareList" v-if="c.id == scope.row.oldercareId">{{ c.level }}</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="newcareId"
            align="center"
            label="新护理级别">
          <template slot-scope="scope">
            <span style="color: green" v-for="c in CareList" v-if="c.id == scope.row.newcareId">{{ c.level }}</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="changedatetime"
            align="center"
            label="更换日期">
        </el-table-column>
        <el-table-column
            prop="changeremark"
            align="center"
            label="备注">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="danger"
                align="center"
                style="margin-top: 3px;"
                @click="handleDelete(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>

      <!--返院对话框-->
      <el-dialog title="返院" :visible.sync="goBackOutFormVisible" @close="closegoBackOutForm"
                 :close-on-click-modal="false">
        <el-form>
          <el-form-item label="返院日期" prop="goBackDatetime">
            <el-date-picker
                v-model="goBackOutForm.backdatetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :editable="false"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closegoBackOutForm">取 消</el-button>
          <el-button type="primary" @click="backgoBackOutFormOK">确 定</el-button>
        </div>
      </el-dialog>
      <!--修改外出申请-->
      <el-dialog title="编辑外出申请" :visible.sync="modifyOutFormVisible" @close="closeModifyOutForm"
                 :close-on-click-modal="false">
        <el-dialog
            width="75%"
            title="选择长者信息"
            :visible.sync="innerVisible"
            append-to-body>
          <el-input placeholder="请输入长者姓名" clearable v-model="olderName" @clear="searchOlder">
            <el-button slot="append" icon="el-icon-search" @click="searchOlder"></el-button>
          </el-input>
          <el-pagination
              @size-change="olderSizeChange"
              @current-change="olderCurrentChange"
              :current-page="olderPageNum"
              :page-sizes="[3, 6 , 12, 24]"
              :page-size="olderPageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="olderTotal"
          ></el-pagination>
          <template>
            <el-table
                :data="olderList"
                style="width: 100%"
                :row-class-name="tableRowClassName"
            >
              <el-table-column
                  label="编号"
                  type="index"
                  width="60">
              </el-table-column>
              <el-table-column
                  prop="name"
                  label="长者姓名"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="sex"
                  label="性别"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="bed.bedname"
                  label="床位号"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="idcard"
                  label="长者身份证号"
                  width="220">
              </el-table-column>
              <el-table-column
                  prop="checkdate"
                  label="入住日期"
                  width="150">
              </el-table-column>
              <el-table-column
                  label="在院状态"
                  width="120"
              >
                <template slot-scope="scope">
                  <span style="color: red" v-if="scope.row.status == 0">退房</span>
                  <span style="color: green" v-if="scope.row.status == 1">入住</span>
                  <span style="color: red" v-if="scope.row.status == 2">待退房</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="240">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      v-if="scope.row.status != 0"
                      @click="selectOlder(scope.row.id,scope.row.name)">选择
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-dialog>
        <el-form ref="modifyOutForm" :rules="modifyOutFormRules" :model="modifyOutForm" label-width="150px" size="mini">
          <el-form-item label="长者" prop="name" @click="innerVisible = true">
            <el-button type="primary" v-model="modifyOutForm.olderId" @click="innerVisible = true">
              {{ modifyOutForm.older.name }}
            </el-button>
          </el-form-item>

          <el-form-item label="外出原因" prop="outcause">
            <el-input v-model="modifyOutForm.outcause"></el-input>
          </el-form-item>
          <el-form-item label="外出日期" prop="outdatetime">
            <el-date-picker
                v-model="modifyOutForm.outdatetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :editable="false"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注" prop="outremark">
            <el-input v-model="modifyOutForm.outremark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeModifyOutForm">取 消</el-button>
          <el-button type="primary" @click="modifyOutFormOK">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {toRefs} from "vue";
import {clear} from "core-js/internals/task";

export default {
  computed: {
    clear() {
      return clear
    }
  },
  data() {
    return {
      olderCareName:'',
      olderName: '',
      olderList: [],
      innerVisible: false,
      addChangeForm: {
        olderId: 0,
        oldercareId:null,
        newcareId: '',
        changedatetime: '',
        changeremark: '',
        name: '',
      },
      modifyOutForm: {
        olderId: 0,
        outcause: '',
        outdatetime: '',
        outremark: '',
        older: {
          name: '',
        },
      },
      ChangeCareList: [],
      CareList: [],
      modifyOutFormVisible: false,
      goBackOutFormVisible: false,
      addChangeFormVisible: false,
      name: '',
      pageNum: 1,
      olderPageNum: 1,
      pageSize: 3,
      olderPageSize: 3,
      total: 0,
      olderTotal: 0,
      addChangeFormRules: {
        newcareId: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        changedatetime: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
      modifyOutFormRules: {
        outcause: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        outdatetime: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
      goBackOutForm: {
        id: 0,
        backdatetime: '',
      },
    };
  },
  methods: {
    tableRowClassName({rowIndex}) {
      if (rowIndex === 0) {
        return 'white-row'; // 第一行为白色
      } else if (rowIndex % 4 === 1) {
        return 'warning-row'; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 2 === 0) {
        return ''; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 3 === 2) {
        return 'white-row'; // 第三行和每隔三行后的行为白色
      } else {
        return 'success-row'; // 每隔三行后的行为黄色
      }
    },

    handleDelete(id) {
      this.$confirm("你确定要删除此记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = '/change/deleteChange/' + id;
        this.$axios.delete(url).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: 'success',
              duration: 1000
            });
            this.initChangeCareList();
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error',
              duration: 1000
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "取消成功"
        })
      })

    },
    openAddChange() {
      this.addChangeFormVisible = true;
      this.addChangeForm = {};
    },
    closeAddChangeForm() {
      this.addChangeFormVisible = false;
    },
    //打开返院修改框
    handleGoBack(id) {
      this.goBackOutFormVisible = true;
      this.goBackOutForm.id = id;
    },
    //选择长者
    selectOlder(id, name) {
      this.modifyOutForm.olderId = id;
      this.modifyOutForm.older.name = name;
      this.innerVisible = false;
    },
    selectOlderAdd(id, name) {
      let url = '/change/selectOlderCareIdById/' + id;
      let i;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          i = res.data.data;
          this.$message({
            message: res.data.msg,
            type: 'success',
            duration: 1000
          });
          this.$set(this.addChangeForm, 'olderId', id);
          this.$set(this.addChangeForm, 'oldercareId',res.data.data.id);
          this.$set(this.addChangeForm, 'name', name);
          this.$set(this.addChangeForm, 'olderCareName',res.data.data.level);
         console.log(this.addChangeForm)
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
      console.log(this.addChangeForm)
      this.innerVisible = false;
    },
    //修改申请表
    handleEdit(id) {
      this.modifyOutFormVisible = true;
      let url = "/outApplication/selectOutApplication/" + id;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.modifyOutForm = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //确认添加
    addChangeFormOK() {
      this.$refs.addChangeForm.validate((vaild) => {
        if (this.addChangeForm.olderId == null) {
          alert("请选择更换人员")
          return;
        }
        if (!vaild) {
          return
        } else {
          this.$confirm("你确定要更换吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            let url = '/change/addChange'
            this.$axios.post(url, this.addChangeForm).then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                this.initChangeCareList();
                this.addChangeFormVisible = false;
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
          }).catch(() => {
            this.$message({
              type: "info",
              message: "取消成功"
            })
          })
        }
      })
    },
    //确认修改
    modifyOutFormOK() {
      this.$refs.modifyOutForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          let url = '/outApplication/modifyOutApplication'
          this.$axios.put(url, this.modifyOutForm).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.initChangeCareList();
              this.modifyOutFormVisible = false;
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error',
                duration: 1000
              });
            }
          })
        }
      })
    },
    //确认返院
    backgoBackOutFormOK() {
      let url = '/outApplication/modifyOutApplicationStatus/' + this.goBackOutForm.id + "/" + this.goBackOutForm.backdatetime
      alert(this.goBackOutForm.backdatetime);
      this.$axios.put(url).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.msg,
            type: 'success',
            duration: 1000
          });
          this.initChangeCareList();
          this.goBackOutFormVisible = false;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //关闭返院对话框
    closegoBackOutForm() {
      this.goBackOutFormVisible = false;
      this.goBackOutForm = {};
    },
    //关闭修改对话框
    closeModifyOutForm() {
      this.modifyOutFormVisible = false;
    },
    //查询长者
    search() {
      this.pageNum = 1;
      this.initChangeCareList();
    },
    clearSearch() {
      this.name = '';
      this.pageNum = 1;
      this.initChangeCareList();
    },
    //长者内置搜收框
    searchOlder() {
      this.olderPageNum = 1;
      this.initOlderList();
    },
    //初始化长者信息
    initOlderList() {
      let url = "/Older/list"
      this.$axios.get(url, {
        params: {
          name: this.olderName,
          now: this.olderPageNum,
          size: this.olderPageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.olderList = res.data.data.list;
          this.olderPageNum = res.data.data.pageNum;
          this.olderPageSize = res.data.data.pageSize;
          this.olderTotal = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },

    //初始化查询护理级别数据
    initCareList() {
      let url = "/care/selectAllListYsw"
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.CareList = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //初始化查询更换护理记录数据
    initChangeCareList() {
      let url = "/changeCareList/list"
      this.$axios.get(url, {
        params: {
          name: this.name,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.ChangeCareList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //改变条数查询
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initChangeCareList();
    },
    //改变长者条数查询
    olderSizeChange(size) {
      this.olderPageSize = size;
      this.olderPageNum = 1;
      this.initOlderList();
    },
    //改变页数查询
    handleCurrentChange(now) {
      this.pageNum = now;
      this.initChangeCareList();
    },
    //改变长者页数查询
    olderCurrentChange(now) {
      this.olderPageNum = now;
      this.initOlderList();
    },
  },
  created() {
    this.initChangeCareList();
    this.initOlderList();
    this.initCareList();
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

</style>